<?php

namespace app\store\controller\apps\points;

use app\store\model\points\Order as OrderModel;
use app\store\model\Express as ExpressModel;
use app\store\controller\Controller;

/**
 * 积分商城订单管理
 * Class Order
 * @package app\store\controller\apps\points
 */
class Order extends Controller
{
    /**
     * 全部订单列表
     * @param string $dataType
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index($dataType = 'all')
    {
        // 订单列表
        $model = new OrderModel;
        $list = $model->getList($dataType, $this->request->get());
        return $this->fetch('index', compact('dataType', 'list'));
    }

    /**
     * 订单详情
     * @param $order_id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function detail($order_id)
    {
        return $this->fetch('detail', [
            'detail' => OrderModel::detail($order_id),
            'express_list' => ExpressModel::getAll()
        ]);
    }

    /**
     * 确认发货
     * @param $order_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function delivery($order_id)
    {
        $model = OrderModel::detail($order_id);
        if ($model->delivery($this->postData('order'))) {
            return $this->renderSuccess('发货成功');
        }
        return $this->renderError($model->getError() ?: '发货失败');
    }

}
